package com.sgb.businessmodule.view.ui.adapter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.businessmodule.BusinessConfig;
import com.sgb.businessmodule.entity.HomeBusinessListEntity;
import com.swgk.core.BuildConfig;
import com.swgk.core.base.aroute.ARouterStartWebBase;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.MLog;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;

/**
 * @author 张驰
 * @date 2021/8/17 21:44
 * @Description 商机跳转
 * @name com.sgb.businessmodule.view.ui.adapter
 */
public class AdapterJumpPush {

    public CallBack iCallBack;
    private String detail = "";

    public interface CallBack {
        void doBack();
    }

    public static final AdapterJumpPush getInstance(Context context, HomeBusinessListEntity entity, CallBack iCallBack) {
        return new AdapterJumpPush(context, entity, iCallBack);
    }

    public AdapterJumpPush(Context context, HomeBusinessListEntity entity, CallBack iCallBack) {
        this.iCallBack = iCallBack;
        toJumpClass(context, entity);
    }

    private void toJumpClass(Context context, HomeBusinessListEntity entity) {
        int siteType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 1);
        String token = "";
        String id = entity.getBusinessNo();
        String param = "";
        String urlParam = "";
        switch (entity.getSource()) {
            case 10: //知产发布-商标交易
                detail = "商标";
                ARouter.getInstance()
                        .build(AppRouteConstant.ZiChan.ZICHAN_MAKEOVERDETAILACTIVITY)
                        .withString("detaildata", detail)
                        .withString("no", id)
                        .navigation();
                return;
            case 11: //知产发布-专利交易
                detail = "专利";
                ARouter.getInstance()
                        .build(AppRouteConstant.ZiChan.ZICHAN_MAKEOVERDETAILACTIVITY)
                        .withString("detaildata", detail)
                        .withString("no", id)
                        .navigation();
                return;
            case 12: //知产发布-版权交易
                detail = "版权";
                ARouter.getInstance()
                        .build(AppRouteConstant.ZiChan.ZICHAN_MAKEOVERDETAILACTIVITY)
                        .withString("detaildata", detail)
                        .withString("no", id)
                        .navigation();
                return;
            case 13: //需求发布-求购商标
            case 14: //需求发布-求购专利
            case 15: //需求发布-求购版权
            case 16: //需求发布-求购服务
                ARouter.getInstance()
                        .build(AppRouteConstant.QiYeFuWu.ROUTER_PATH_NEWNEEDDETAILSACTIVITY)
                        .withString("data", id)
                        .navigation();
                return;
            case 17: //客户询价
                param = "enquiryDetail";
                urlParam = "no=" + id;
                break;
            case 18: //招标公告-商品&材料
            case 19: //招标公告-房产租赁
            case 20: //招标公告-设备租赁
            case 21: //招标公告-车辆租赁
            case 22: //招标公告-劳务分包
            case 23: //招标公告-专业工程
            case 24: //招标公告-工程总包
            case 25: //招标公告-PPP项目
            case 57://招标公告-机械租赁
                ARouter.getInstance()
                        .build(AppRouteConstant.ZhaoCai.ROUTER_PATH_ZC_TENDERDETAILSACTIVITY)
                        .withString("NO", id)
                        .withInt("type", 0)
                        .navigation();
                return;
            case 26: //竞价采购-商品&材料
            case 27: //竞价采购-机械租赁
            case 28: //竞价采购-设备租赁
            case 29: //竞价采购-房产租赁
            case 30: //竞价采购-车辆租赁
            case 31: //竞价采购-劳务分包
            case 32: //竞价采购-专业工程
            case 33: //竞价采购-工程总包
            case 34: //竞价采购-PPP项目
                param = "competitivePurchase";
                String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                urlParam = "id=" + id + "&lng=" + lng + "&lat=" + lat;
                break;
            case 38: //求租-机械求租http://mobile.gcw.net/mechanicsRentDetails?id=1427827063200550912
            case 39: //求租-设备求租http://mobile.gcw.net/equipmentRentDetails?id=1427818678061240320
            case 40: //求租-车辆求租
            case 41: //求租-房产求租
            case 42: //求租-土地求租
            case 43: //求租-工厂求租
            case 44: //求租-农机求租
                param = getMechanicsPash(entity.getSource() - 37);
                urlParam = "id=" + id;
                break;
            case 45: //临时用工
                checkEmploymentApply(context, entity);
                return;
            case 46:  //承运找货-货运发布
                goodsSourceStatusCheck(context, entity);
                return;
            case 47://货主找车-承运发布
                //CarriageDetailActivity承运详情
                ARouter.getInstance()
                        .build(AppRouteConstant.WuLui.ROUTER_PATH_VEHICLEDETAILACTIVITY)
                        .withString("carrierNo", entity.getBusinessNo())
                        .withInt("carriageTypes", 2)
                        .navigation();
                return;
            case 35: //集采报价-需方发起-即报即采-商品材料
            case 36: //集采报价-需方发起-汇总集采-商品材料
            case 37: //集采报价-企业专项-即报即采-商品材料
            case 48: //集采报价-企业专项-汇总集采-商品材料
            case 49: //集采报价-需方发起-即报即采-机械租赁
            case 50: //集采报价-需方发起-汇总集采-机械租赁
            case 51: //集采报价-需方发起-即报即采-设备租赁
            case 52: //集采报价-需方发起-汇总集采-设备租赁
            case 53: //集采报价-企业专项-即报即采-机械租赁
            case 54: //集采报价-企业专项-汇总集采-机械租赁
            case 55: //集采报价-企业专项-即报即采-设备租赁
            case 56: //集采报价-企业专项-汇总集采-设备租赁
                token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
                param = "collection";
                urlParam = "id=" + id + "&type=0&token=" + token;
                break;
            default:
                break;
        }
        getUrlSetting(context, id, param, urlParam);
    }

    /**
     * 根据路由跳转H5详情
     *
     * @param id    当前条目的id
     * @param param 访问当前H5详情的参数
     */
    public void getUrlSetting(Context context, String id, String param, String urlParam) {
        String url = BuildConfig.BASE_URL + "v1/app/common/getUrlSetting";
        BusinessConfig.getInstance().getApi().getUrlSetting(url, param).enqueue(
                new ICallBack<BaseEntity<H5ReEntity>>() {
                    @Override
                    public void onSuccess(BaseEntity<H5ReEntity> data) {
                        String url = data.getData().getUrl() + "?" + urlParam;
                        MLog.d("H5URL: " + url);
                        ARouterStartWebBase.start(url, id);
                    }
                }
        );
    }

    /**
     * 租赁获取H5路由标识
     *
     * @param type 设备类型 1 机械 2 设备 3 车辆 4 房产 5土地 6工厂 7农机
     * @return 返回路由
     */
    private String getMechanicsPash(int type) {
        String name = "";
        if (1 == type) {
            name = "mechanicsRentDetails";//机械
        } else if (2 == type) {
            name = "equipmentRentDetails";//设备
        } else if (3 == type) {
            name = "vehicleRentDetails";//车辆
        } else if (4 == type) {
            name = "houseRentDetails";//房产
        } else if (5 == type) {
            name = "landRentDetails";//土地
        } else if (6 == type) {
            name = "factoryRentDetails";//工厂
        } else if (7 == type) {
            name = "farmRentDetails";//农机
        }
        return name;
    }

    /**
     * 临时用工-校验货源是否报名
     *
     * @param entity
     */
    public void checkEmploymentApply(Context context, HomeBusinessListEntity entity) {
        if (TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""))) {
            jump(entity, false);
            return;
        }
        String no = entity.getBusinessNo();
        BusinessConfig.getInstance().getApi().checkEmploymentApply(no).enqueue(new ICallBack<BaseEntity<Boolean>>() {
            @Override
            public void onSuccess(BaseEntity<Boolean> data) {
                jump(entity, data.getData());
            }
        });
    }

    private void jump(HomeBusinessListEntity entity, boolean isIsApply) {
        //                EmploymentDetailActivity employmentEntity.getProjectName(),employmentEntity.isIsApply()
        ARouter.getInstance()
                .build(AppRouteConstant.GongDui.ROUTER_PATH_EMPLOYMENTDETAILACTIVITY)
                .withString("no", entity.getBusinessNo())
                .withString("teamName", entity.getTypeName())
                .withBoolean("isApply", isIsApply)
                .navigation();
    }

    /**
     * 校验物流是否有效
     *
     * @param entity
     */
    public void goodsSourceStatusCheck(Context context, HomeBusinessListEntity entity) {
        HashMap map = new HashMap();
        map.put("no", entity.getBusinessNo());
        BusinessConfig.getInstance().getApi().goodsSourceStatusCheck(map).enqueue(new ICallBack<BaseEntity<BaseEntity>>() {
            @Override
            public void onSuccess(BaseEntity<BaseEntity> data) {
                checkGoodsState(context, data.getData(), entity);
            }
        });
    }

    public void checkGoodsState(Context context, BaseEntity stateEntity, HomeBusinessListEntity entity) {
        if (goodsIsGone(stateEntity)) {
            new RoundDialog.Builder(context)
                    .title("提示")
                    .content("该货源不存在，请选择其他货源接单")
                    .singleBtn(true)
                    .confirm("知道了")
                    .confirmCallback(v -> {
                        if (iCallBack != null) {
                            iCallBack.doBack();
                        }
                    })
                    .show();
        } else if (goodsIsReceipted(stateEntity)) {
            new RoundDialog.Builder(context)
                    .title("提示")
                    .content("该货源已被接单，请选择其他货源接单")
                    .singleBtn(true)
                    .confirm("知道了")
                    .confirmCallback(v -> {
                        if (iCallBack != null) {
                            iCallBack.doBack();
                        }
                    })
                    .show();
        } else {
            // 数据源取得是普通货运，故不存在竞价货运类型
//            if (entity.getPattern() == 1) {
//                //FreightPrjDetailActivity.start(getActivity(), goodsInfoAdapter.getData().get(position).getNo(),false, false);
//                ARouter.getInstance()
//                        .build(AppRouteConstant.WuLui.ROUTER_PATH_FREIGHTPRJDETAILACTIVITY)
//                        .withString("sourceNo", entity.getBusinessNo())
//                        .withBoolean("goodsType", false) // 是否是订单详情
//                        .withBoolean("position", false)  // 是否是个人中心
//                        .navigation();
//            } else {
            //GeneralPrjDetailActivity.start(getActivity(), goodsInfoAdapter.getData().get(position).getNo(), goodsInfoAdapter.getData().get(position).getPattern(), false);
            ARouter.getInstance()
                    .build(AppRouteConstant.WuLui.ROUTER_PATH_GENERALPRJDETAILACTIVITY)
                    .withString("sourceNo", entity.getBusinessNo())
                    .withInt("goodsType", /*entity.getPattern()*/ 0 /*固定为普通模式*/) //goodstype竞价1和普通0
                    .withBoolean("position", false) //position大厅false和个人中心ture
                    .navigation();
        }
    }

    public boolean goodsIsGone(BaseEntity stateEntity) {
        return "225".equals(stateEntity.getCode()) || "223".equals(stateEntity.getCode()) || "230".equals(stateEntity.getCode());
    }

    public boolean goodsIsReceipted(BaseEntity stateEntity) {
        return "224".equals(stateEntity.getCode()) || "201".equals(stateEntity.getCode());
    }
}
